package javabase.demo02;

import java.util.*;

public class CollectionExercises {

    public static void main(String[] args) {
        // 练习1: 统计单词频率
        String text = "java is a programming language java is widely used java is popular";
        Map<String, Integer> wordFrequency = countWordFrequency(text);
        System.out.println("单词频率: " + wordFrequency);

        // 练习2: 找出两个列表的共同元素
        List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> list2 = Arrays.asList(4, 5, 6, 7, 8);
        List<Integer> commonElements = findCommonElements(list1, list2);
        System.out.println("共同元素: " + commonElements);


        // 练习3: 实现LRU缓存
        LRUCache<Integer, String> lruCache = new LRUCache<>(3);
        lruCache.put(1, "A");
        lruCache.put(2, "B");
        lruCache.put(3, "C");
        System.out.println("初始缓存: " + lruCache);

        lruCache.get(1); // 访问1，使其成为最近使用的
        lruCache.put(4, "D"); // 添加新元素，应该淘汰最久未使用的2
        System.out.println("添加第四个元素后: " + lruCache);

        // 练习4: 对学生按成绩排序
        List<Student> students = new ArrayList<>();
        students.add(new Student("Alice", 85));
        students.add(new Student("Bob", 92));
        students.add(new Student("Charlie", 78));
        students.add(new Student("Diana", 92)); // 相同成绩

        // 按成绩降序，成绩相同按姓名升序
        students.sort((s1, s2) -> {
            int scoreCompare = Integer.compare(s2.score, s1.score); // 降序
            if (scoreCompare != 0) {
                return scoreCompare;
            }
            return s1.name.compareTo(s2.name); // 升序
        });

        System.out.println("按成绩排序的学生:");
        students.forEach(System.out::println);
    }

    // 统计文本中单词频率
    public static Map<String, Integer> countWordFrequency(String text) {
        Map<String, Integer> frequencyMap = new HashMap<>();
        String[] words = text.split("\\s+");

        for (String word : words) {
            frequencyMap.put(word, frequencyMap.getOrDefault(word, 0) + 1);
        }

        return frequencyMap;
    }

    // 找出两个列表的共同元素
    public static List<Integer> findCommonElements(List<Integer> list1, List<Integer> list2) {
        Set<Integer> set1 = new HashSet<>(list1);
        Set<Integer> commonSet = new HashSet<>();

        for (Integer num : list2) {
            if (set1.contains(num)) {
                commonSet.add(num);
            }
        }

        return new ArrayList<>(commonSet);
    }
}
// 简单的LRU缓存实现
class LRUCache<K, V> extends LinkedHashMap<K, V> {
    private final int capacity;

    public LRUCache(int capacity) {
        super(capacity, 0.75f, true);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return size() > capacity;
    }
}

// 学生类
class Student {
    String name;
    int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    @Override
    public String toString() {
        return name + ":" + score;
    }
}